<?php

namespace App\Models\Trip\TripOrderCosts;

use Eloquent;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

/**
 * App\Models\Trip\TripOrderCosts\TripOrderCosts
 *
 * @property int         $id
 * @property string      $user_id   用户id
 * @property string      $trip_id   行程业务id
 * @property int         $trip_type 出行类型 1火车 2飞机 3 酒店
 * @property string      $order_sn  订单单号
 * @property int         $type      如:保险费,人工出票,维语用语言包,提示信息写到配置文件
 * @property string      $price     费用单价
 * @property int         $count     数量
 * @property string      $name      收费项如 疫情隔离补贴,指定车厢.指定铺席,维语用语言包
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property int|null    $trip_no   行程序号 - 1 第一程 - 2 第二程
 * @property string|null $detail    如 二等座，险种，指定的铺席坐席内容等
 * @method static Builder|TripOrderCost newModelQuery()
 * @method static Builder|TripOrderCost newQuery()
 * @method static Builder|TripOrderCost query()
 * @method static Builder|TripOrderCost whereCount($value)
 * @method static Builder|TripOrderCost whereCreatedAt($value)
 * @method static Builder|TripOrderCost whereDetail($value)
 * @method static Builder|TripOrderCost whereId($value)
 * @method static Builder|TripOrderCost whereName($value)
 * @method static Builder|TripOrderCost whereOrderSn($value)
 * @method static Builder|TripOrderCost wherePrice($value)
 * @method static Builder|TripOrderCost whereTripId($value)
 * @method static Builder|TripOrderCost whereTripNo($value)
 * @method static Builder|TripOrderCost whereTripType($value)
 * @method static Builder|TripOrderCost whereType($value)
 * @method static Builder|TripOrderCost whereUpdatedAt($value)
 * @method static Builder|TripOrderCost whereUserId($value)
 * @property string $pay_sn 支付单号
 * @property int $parent_type 父类型,可能与子类型一致
 * @method static Builder|TripOrderCost whereParentType($value)
 * @method static Builder|TripOrderCost wherePaySn($value)
 * @property int|null $confirm_state  价格确认状态 1 已确认 0 待确认
 * @method static Builder|TripOrderCost whereConfirmState($value)
 * @mixin Eloquent
 */
class TripOrderCost extends Model
{
    use HasFactory;

    const COST_STATE_CONFIRMED = 1; // 已确认
    const COST_STATE_TBC       = 0; // 待确认

    const COST_TYPE_TICKET                             = 1; // 票价
    const COST_TYPE_TICKET_SERVICE_FEE                 = 2; // 购票服务费
    const COST_TYPE_SELECT_SEAT                        = 3; // 选座费
    const COST_TYPE_INSURE                             = 4; // 保险费
    const COST_TYPE_CHANGE_SERVICE_FEE                 = 5; // 改签服务费
    const COST_TYPE_SNAP_HIGH_LEVEL_SERVICE_FEE        = 6; // 高速抢票费
    const COST_TYPE_SNAP_EDIT_TICKET_PRICE_DIFF        = 7; // 编辑抢票坐席差价
    const COST_TYPE_PLANE_CONSTRUCTION_FEE             = 8; //  机建费
    const COST_TYPE_PLANE_FUEL_FEE                     = 9; //  燃油费
    const COST_TYPE_PLANE_CHANGE_FEE                   = 10;//  改签手续费
    const COST_TYPE_PLANE_CHANGE_UPGRADE_FEE           = 11;//  改签升舱费
    const COST_TYPE_PLANE_CHANGE_DIFF_TICKET           = 12;//  差价 - 票面差价
    const COST_TYPE_PLANE_CHANGE_DIFF_CONSTRUCTION_FEE = 13;//  差价 - 机建费
    const COST_TYPE_PLANE_CHANGE_DIFF_FUEL_FEE         = 14;//  差价 - 燃油费
    const COST_TYPE_UNIT
                                                       = [
            self::COST_TYPE_TICKET                 => "张",
            self::COST_TYPE_TICKET_SERVICE_FEE     => "份",
            self::COST_TYPE_SELECT_SEAT            => "份",
            self::COST_TYPE_INSURE                 => "份",
            self::COST_TYPE_CHANGE_SERVICE_FEE     => "份",
            self::COST_TYPE_PLANE_CONSTRUCTION_FEE => "份",
            self::COST_TYPE_PLANE_FUEL_FEE         => "份",
        ];
    const COST_TYPE_DESC
                                                       = [
            self::COST_TYPE_TICKET                             => "车票",
            self::COST_TYPE_TICKET_SERVICE_FEE                 => "购票手续费",
            self::COST_TYPE_SELECT_SEAT                        => "优选铺席",
            30                                                 => "优选铺席",// 组合值 3 + seat_type
            31                                                 => "优选座席",// 组合值 3_1
            32                                                 => "优选铺席",// 组合值 3_2
            self::COST_TYPE_INSURE                             => "保险费",
            self::COST_TYPE_CHANGE_SERVICE_FEE                 => "改签手续费",
            self::COST_TYPE_SNAP_HIGH_LEVEL_SERVICE_FEE        => "购买高速抢票服务",
            self::COST_TYPE_SNAP_EDIT_TICKET_PRICE_DIFF        => "编辑抢票坐席差价",
            self::COST_TYPE_PLANE_CONSTRUCTION_FEE             => "机建费",
            self::COST_TYPE_PLANE_FUEL_FEE                     => "燃油费",
            self::COST_TYPE_PLANE_CHANGE_FEE                   => "改签手续费",
            self::COST_TYPE_PLANE_CHANGE_UPGRADE_FEE           => "升舱费",
            self::COST_TYPE_PLANE_CHANGE_DIFF_TICKET           => "机票差价", // 可能存在相同舱位 价格不同,不能作为升舱费
            self::COST_TYPE_PLANE_CHANGE_DIFF_CONSTRUCTION_FEE => "机建费差价",
            self::COST_TYPE_PLANE_CHANGE_DIFF_FUEL_FEE         => "燃油费差价",
        ];
    protected $table = 'trip_order_costs';
    protected $fillable
                     = [
            'user_id',
            'trip_id',
            'trip_type',
            'trip_no',
            'order_sn',
            'pay_sn',
            'type',
            'parent_type', // 父类型 比如编辑的票价差额属于 票价 , 当发生退票价差价时  需要按比例计算
            'price',
            'count',
            'name',
            'detail',
            'confirm_state',
        ];

    /**
     * 类型 是否存在 多条的可能性 , 如果存在目前能考虑到的情况就是
     * 有多次支付业务 引入的新明细 本次只支持抢票编辑 票价差
     * @param int $type
     * @return bool
     */
    public static function mayHasMoreRecord(int $type): bool
    {
        return $type == self::COST_TYPE_SNAP_EDIT_TICKET_PRICE_DIFF;
    }

    public function getPaySn(): string
    {
        return $this->getAttribute('pay_sn');
    }

    public function getParentType(): int
    {
        return $this->getAttribute('parent_type');
    }

    public function getType(): int
    {
        return $this->getAttribute('type');
    }

    public function getConfirmState(): int
    {
        return $this->getAttribute('confirm_state');
    }

    public function getName(): string
    {
        return $this->getAttribute('name');
    }

    /**
     * 单价
     * @return float
     */
    public function getPrice(): float
    {
        return $this->getAttribute('price');
    }

    /**
     * 数量
     * @return int
     */
    public function getCount(): int
    {
        return $this->getAttribute('count');
    }
}
